package com.sc.nft.dao;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.entity.vo.DaoWalletVO;
import com.sc.nft.exception.GlobalRunTimeException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.entity.DaoWallet;
import com.sc.nft.mapper.DaoWalletMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;

/**
 * dao钱包表(sc_dao_wallet)数据DAO
 *
 * @author zxy
 * @since 2023-12-16 19:17:50
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class DaoWalletDao extends ServiceImpl<DaoWalletMapper, DaoWallet> {

    public DaoWallet getByDaoId(Long daoId){
        DaoWallet wallet =  lambdaQuery().eq(DaoWallet::getDaoId,daoId).one();
        Assert.notNull(wallet,()->new GlobalRunTimeException("DAO钱包不存在"));
        return wallet;
    }

    public void addBalance(Long daoId, BigDecimal amount){
        UpdateWrapper<DaoWallet> wrapper = new UpdateWrapper<>();
        wrapper.setSql("sum_income=sum_income+"+amount+" , balance=balance+"+amount);
        wrapper.eq("dao_id",daoId);
        update(wrapper);
    }
    public void subBalance(Long daoId, BigDecimal amount){
        UpdateWrapper<DaoWallet> wrapper = new UpdateWrapper<>();
        wrapper.setSql("balance=balance-"+amount);
        wrapper.eq("dao_id",daoId);
        update(wrapper);
    }

    public void pass(Long daoId, BigDecimal amount){
        UpdateWrapper<DaoWallet> wrapper = new UpdateWrapper<>();
        wrapper.setSql("freeze_amount=freeze_amount-"+amount);
        wrapper.eq("dao_id",daoId);
        update(wrapper);
    }
    public void approval(Long daoId, BigDecimal amount){
        UpdateWrapper<DaoWallet> wrapper = new UpdateWrapper<>();
        wrapper.setSql("freeze_amount=freeze_amount+"+amount+" , balance=balance-"+amount);
        wrapper.eq("dao_id",daoId);
        update(wrapper);
    }

    public void refuse(Long daoId, BigDecimal amount){
        UpdateWrapper<DaoWallet> wrapper = new UpdateWrapper<>();
        wrapper.setSql("freeze_amount=freeze_amount-"+amount+" , balance=balance+"+amount);
        wrapper.eq("dao_id",daoId);
        update(wrapper);
    }

    public Page<DaoWalletVO> getAdminPage(String name,Page toMp) {
        return baseMapper.getAdminPage(toMp,name);
    }
    public DaoWalletVO getAdminDetail(Long daoId) {
        return baseMapper.getAdminDetail(daoId);
    }
}